//index.js
//获取应用实例
const app = getApp();
const recorderManager = wx.getRecorderManager();
var QQMapWX = require('../../../libs/qqmap-wx-jssdk.js');
var common = require('../../../libs/common.js');
var md5 = require('../../../libs/md5.js');
var qqmapsdk;
var myaudio = wx.createInnerAudioContext();

Page({
  data: {
    banner:'',
    showpicker:0,//显示车型选择
    chexing: [],
    aiche:'',//我的选择
    index:'',//
    luyinpop:false,//css
    luyinzhong:true,//css
    is_clock: false,
    startPoint:'',
    files: [],//图片路径
    videosrc:[],//视频
    chooseimg:false,
    choosevideo:false,
    yiluyin:false,
    yuyinsrc: [],
    luyintime: [],
    address:'',
    activity_location: "",
    latitude: 0,//地图初次加载时的纬度坐标
    longitude: 0, //地图初次加载时的经度坐标
    dialogShow: false,
    showOneButtonDialog: false,
    buttons: [{ text: '取消' }, { text: '确定' }],
    oneButton: [{ text: '确定' }],
    on_demand:'',
    demanddetail:'',//需求详情
    demandadd:'',//补充需求
    bjshangjia:[],
    playing:[],
    playing2:[],
    setinter:'',
    oneche:false,
    detail:'',
    tixingshow:false,
    videofrom:'',
    previewvideo:false,
    finishShow:false,
    pingjiaShow:false,
    shop_id:'' //评价时shop_id
  },
 
  onLoad: function (options) {
    this.getbanner();
    this.getopenid();
    this.qqmap();
    this.luyin = this.selectComponent("#luyin");
    this.getdetail();
  },
  onShow:function(){
    this.getdetail();
    if (app.globalData.detail == '') {
      var userid = wx.getStorageSync('userId');
      this.getcarlist(userid);
      this.geton(userid);
    }
    
  },

  getdetail:function(){
    if (app.globalData.detail != '') {
      var detail = app.globalData.detail;
      var aiche = detail.demand.kind;
      var chexing = this.data.chexing;
      console.log(chexing)
      for(var x in chexing){
        if(chexing[x].kind == aiche){
            console.log(x)
            this.setData({
              index:x
            })
        }
      }
      if (detail.demand.imgs.length > 0){
          this.setData({
            chooseimg:true
          })
      }
      if (detail.demand.video.length > 0){
          this.setData({
            choosevideo:true
          })
      }
      if (detail.demand.voice.length > 0){
        var voicelen = detail.demand.voice.length;
        var playing = this.data.playing;
        for(var i = 0;i< voicelen;i++){
          playing[i]=0;
        }
      }
      console.log(1111)
      console.log(detail.demand)
      this.setData({
        detail: detail,
        address: detail.demand.address,
        latitude: detail.demand.latitude,
        longitude: detail.demand.longitude,
        yuyinsrc: detail.demand.voice,
        luyintime: detail.demand.voice_len,
        videosrc:detail.demand.video,
        files: detail.demand.imgs,
        playing:playing
      })
    }
  },

/***获取默认地址****/
qqmap:function(){
  // 实例化API核心类
  qqmapsdk = new QQMapWX({
    //此key需要用户自己申请
    key: 'IHABZ-DOH3O-NMGWL-SORKW-F7OQ2-HCFAQ'
  });
  var that = this;
  // 调用接口
  qqmapsdk.reverseGeocoder({
    success: function (res) {
      console.log(123456)
      let addrObj = res.result;
      that.setData({
        address: addrObj.address_component.city +addrObj.address_component.district,
        activity_location: res.result.address,
        latitude:res.result.location.lat,
        longitude:res.result.location.lng
      });
      // console.log(that.data.address);
    },
    fail: function (res) {
      //console.log(res);
    },
    complete: function (res) {
      //console.log(res);
    }
  });
},

/***获取openid***/
getopenid:function(){ 
  var that = this;
  wx.login({
    success(res) {
      if (res.code) {
          var login={
            url:'api/userr/wexinLogin',
            data:{
              code:res.code
            },
            success:function(e){
              var userid = e.data.data.user_id;
              var status = e.data.data.status;
              var shop_id = e.data.data.shop_id;
              var role = e.data.data.role;
              var shop_type = e.data.data.shop_type;
              wx.setStorageSync('shop_id',shop_id);
              that.geton(userid);
              that.getcarlist(userid);
            }
          };
          app.post(login);
      } else {
        console.log('登录失败！' + res.errMsg)
      }
    }
  })
},
/*****获取banner*****/ 
getbanner:function(){
  var shop_type = wx.getStorageSync('shop_type');
  var that = this;
  var banner = {
    url: 'api/index/getbanner',
    data:{
      type:shop_type
    },
    success: function (e) {
      if(e.data.data.path){
        var banner = e.data.data.path;
        that.setData({
          banner: banner
        })
      }
     
    }
  };
  app.post(banner);
},

// 获取进行中的需求情况
geton:function(userid){
  var that = this;
  var on = {
    url: 'api/index/getOnDemand',
    data:{
      user_id:userid
    },
    success: function (e) {
      console.log(e)
      var on_demand = e.data.data.on_demand;
      that.setData({
        on_demand:on_demand
      })
      if(on_demand == 1){
        var voicelen = e.data.data.demand.voice.length;
        var playing = that.data.playing;
        var playing2 = that.data.playing2;
        for(var i = 0;i<voicelen;i++){
            playing[i] = 0
        }
        if (e.data.data.demand_add) {
          var voicelen2 = e.data.data.demand_add.voice.length;
          for (var j = 0; j < voicelen2.length; j++) {
            playing2[j] = 0
          }
        }
        
        that.setData({
          demanddetail: e.data.data.demand,
          playing:playing,
          demandadd: e.data.data.demand_add,
          playing2:playing2
        })
        
        that.getbaojia(e.data.data.demand.demand_id);
        that.shuaxin(e.data.data.demand.demand_id);
      }
    }
  };
  app.post(on);
},
// 刷新商家信息
  shuaxin: function(demand_id){
    var that = this;
    that.data.setinter = setInterval(function(){
      that.getbaojia(demand_id);
    },5000)
},
// 获取商家报价信息
getbaojia:function(demand_id){
  var that = this;
  var baojia = {
    url: 'api/index/updateOrder',
    data: {
      demand_id: demand_id
    },
    success: function (e) {
      console.log(e)
      // if (e.data.data.length > that.data.bjshangjia.length){
      //   var num = e.data.data.length - that.data.bjshangjia.length;
        var bjlist = e.data.data;
        // for(var i=num;i>0;i--){
        //    that.data.bjshangjia.unshift(bjlist[i-1])
        // }
        that.setData({
          bjshangjia: bjlist
        })
      // }
    }
  };
  app.post(baojia); 
},

// 获取用户的爱车列表
getcarlist:function(userid){
  var that = this;
  var userid = userid;
  var param ={
    url:"api/user/getCarList",
    data:{
      user_id:userid
    },
    success:function(e){
      if(e.data.data.length == 1){
          that.setData({
            oneche:true
          })
      }
      that.setData({
        chexing: e.data.data
      });
    }
  };
  app.post(param);
},
/******选择爱车******/ 
showPicker: function () {
  if(this.data.chexing.length == 0){
      wx.showToast({
        title: '请先绑定爱车',
        icon:'none',
        duration:3000,
      })
    var status = wx.getStorageSync('status');
    console.log(status);
      if (status == 0){
          setTimeout(function () {
            wx.navigateTo({
              url: '/pages/mine/mobile/mobile',
            })
          }, "2000");
      }else{
        setTimeout(function () {
          wx.navigateTo({
            url: '/pages/mine/bindcar/bindcar',
          })
        }, "2000");
      }
      
  }else{
    this.setData({
      showpicker: 1,
      index: 0
    })
  }
  
},
hidePicker: function () {
  this.setData({
    showpicker: 0
  })
},
bindPickerChange: function (e) {
  console.log(e.detail.value[0])
  this.setData({
    index: e.detail.value[0]
  })
},

/******语音描述-录音******/
  luyintap: function (e) {
    this.luyin.luyin();
  },
  
  // 开始录音
  handleRecordStart: function (e) {
    this.setData({
      is_clock: true,//长按时应设置为true，为可发送状态
      startPoint: e.detail.e.touches[0],//记录触摸点的坐标信息
      luyinzhong:true,//录音跳动
    })
    //设置录音参数
    const options = {
      duration: 300000,
      sampleRate: 16000,
      numberOfChannels: 1,
      encodeBitRate: 48000,
      format: 'mp3'
    }
    //开始录音
    recorderManager.start(options);
    console.log('start');
  },
  // 松开发送
  handleRecordStop: function (e) {
    recorderManager.stop()//结束录音
    
    var that = this
    console.log(that.data.is_clock)
    //此时先判断是否需要发送录音
    
      
        //对停止录音进行监控
        recorderManager.onStop((res) => {
            //对录音时长进行判断，少于2s的不进行发送，并做出提示
          if (that.data.is_clock == true){
                if (res.duration < 2000) {
                  wx.showToast({
                    title: '录音时间太短，请长按录音',
                    icon: 'none',
                    duration: 1000
                  })
                } else {
                  var yuyinsrc = res.tempFilePath;
                  var luyintime = Math.floor(res.duration / 1000);
                  var timestamp = Date.parse(new Date()) / 1000;
                  var random = Math.floor(Math.random() * 100);
                  var sign = 'GHRDY65' + String(random) + String(timestamp);
                  sign = md5.hexMD5(sign);
                  wx.showLoading({
                    title: '上传中',
                  })
                  wx.uploadFile({
                    url: app.static.url + 'api/common/uploadFile',
                    filePath: yuyinsrc,
                    name: 'file',
                    formData: {
                      timestamp: timestamp,
                      random: random,
                      sign: sign
                    },
                    success: function (e) {
                      console.log(e);
                      var data = e.data;
                      data = JSON.parse(data);
                      var yuyins = that.data.yuyinsrc.concat(data.data.url);
                      console.log(yuyins)
                      wx.hideLoading()
                      that.setData({
                        yuyinsrc: that.data.yuyinsrc.concat(data.data.url),
                        luyintime: that.data.luyintime.concat(luyintime) ,
                        playing:that.data.playing.concat(0),
                        yiluyin: true
                      });
                    }
                  })

                }
            }else{
               
               console.log('不发送')
            }
           
        })
  },
  // 滑动取消发送
  handleTouchMove: function (e) {
    //计算距离，当滑动的垂直距离大于25时，则取消发送语音
    if (Math.abs(e.detail.touches[0].clientY - this.data.startPoint.clientY) > 30) {
      this.setData({
        is_clock: false,//设置为不发送语音
        luyinpop:false,
        luyinzhong:false
      })
    }
    console.log('move')
  },
  // 播放录音
  playluyin:function(e){
    console.log(e)
    var datasrc = e.currentTarget.dataset.src;
    var index = e.currentTarget.dataset.index;
    var that = this;
    var playing = that.data.playing;
    var playing2 = that.data.playing2;
    for(var i = 0;i<playing.length ;i++){
      playing[i] = 0;
    }
    for (var i = 0; i < playing2.length; i++) {
      playing2[i] = 0;
    }
    myaudio.pause();
    myaudio.src = datasrc;
    
    if(playing[index] == 0){
      myaudio.play(); 
      playing[index] = 1;
      that.setData({
        playing:playing
      })
      myaudio.onEnded(function(){
        console.log(1)
        playing[index]=0;
        that.setData({
          playing: playing
        })
      })
    }else{
      myaudio.pause();
      playing[index] = 0;
      that.setData({
        playing:playing
      })
    }
    
  },
  playluyin2: function (e) {
    var datasrc = e.currentTarget.dataset.src;
    var index = e.currentTarget.dataset.index;
    var that = this;
    var playing2 = that.data.playing2;
    var playing = that.data.playing;
    for (var i = 0; i < playing2.length; i++) {
      playing2[i] = 0;
    }
    for (var i = 0; i < playing.length; i++) {
      playing[i] = 0;
    }
    myaudio.pause();
    myaudio.src = datasrc;

    if (playing2[index] == 0) {
      myaudio.play();
      playing2[index] = 1;
      that.setData({
        playing2: playing2
      })
      myaudio.onEnded(function () {
        playing2[index] = 0;
        that.setData({
          playing2: playing2
        })
      })
    } else {
      myaudio.pause();
      playing2[index] = 0;
      that.setData({
        playing2: playing2
      })
    }

  },

  delluyin:function(e){
    var that = this;
    var yuyin = that.data.yuyinsrc;
    var luyintime = that.data.luyintime;
    var index = e.currentTarget.dataset.index; //获取当前按图片下标
    wx.showModal({
      title: '提醒',
      content: '确定要删除此录音吗？',
      success: function (res) {
        if (res.confirm) {
          yuyin.splice(index, 1);
          luyintime.splice(index, 1);
        } else if (res.cancel) {
          return false;
        }
        
        that.setData({
          yuyinsrc: yuyin,
          luyintime:luyintime
        });
      }
    })
  },

/****** 选择视频 ******/
choosevideo:function(){
  if (this.data.chexing.length == 0) {
    wx.showToast({
      title: '请先绑定爱车',
      icon: 'none',
      duration: 3000,
    })
    var status = wx.getStorageSync('status');
    console.log(status);
    if (status == 0) {
      setTimeout(function () {
        wx.navigateTo({
          url: '/pages/mine/mobile/mobile',
        })
      }, "2000");
    } else {
      setTimeout(function () {
        wx.navigateTo({
          url: '/pages/mine/bindcar/bindcar',
        })
      }, "2000");
    }

  } else {
  this.uploadvideo();
  }
},
uploadvideo:function(){
    var that = this;
    var videosrc=that.data.videosrc;
    wx.chooseVideo({
      sourceType: ['album', 'camera'],
      sizeType: ['original', 'compressed'],
      maxDuration: 60,
      camera: 'back',
      success(res) {
          console.log(res.tempFilePath);
          if (wx.showLoading) {
            wx.showToast({
              icon: 'loading',
              title: '请稍候…',
              mask: true
            })
          };
          var timestamp = Date.parse(new Date()) / 1000;
          var random = Math.floor(Math.random() * 100);
          var sign = 'GHRDY65' + String(random) + String(timestamp);
          sign = md5.hexMD5(sign);
          wx.uploadFile({
            url: app.static.url + 'api/common/uploadFile',
            filePath: res.tempFilePath,
            name: 'file',
            formData: {
              timestamp: timestamp,
              random: random,
              sign:sign
            },
            success: function (res) {
              console.log(res);

              wx.hideLoading();
              var data = res.data;
              data = JSON.parse(data);
              if (data.code == 1) {
                that.setData({
                  videosrc: that.data.videosrc.concat(data.data.url),
                  choosevideo:true
                });
              }else{
                wx.showToast({
                  icon: 'none',
                  title: data.msg,
                  mask: true
                })
              }
            }
          })
        // 
      }
    })
},

  // 视频删除效果
  closedel: function (e) {
    var that = this;
    var video = that.data.videosrc;
    var index = e.currentTarget.dataset.index; //获取当前按图片下标
    wx.showModal({
      title: '提醒',
      content: '确定要删除此视频吗？',
      success: function (res) {
        if (res.confirm) {
          video.splice(index, 1);
        } else if (res.cancel) {
          return false;
        }
        that.setData({
          videosrc: video
        });
      }
    })

  },
  //长按图片视频
  deleteVideo: function (e) {
    var that = this;
    var video = that.data.videosrc;
    var index = e.currentTarget.dataset.index; //获取当前长按图片下标
    wx.showModal({
      title: '提醒',
      content: '确定要删除此视频吗？',
      success: function (res) {
        if (res.confirm) {
          video.splice(index, 1);
        } else if (res.cancel) {
          return false;
        }
        that.setData({
          videosrc: video
        });
      }
    })
  },
  /*** 选择图片 ***/
  // 添加图片
  chooseImage: function (e) {
    if (this.data.chexing.length == 0) {
      wx.showToast({
        title: '请先绑定爱车',
        icon: 'none',
        duration: 3000,
      })
      var status = wx.getStorageSync('status');
      console.log(status);
      if (status == 0) {
        setTimeout(function () {
          wx.navigateTo({
            url: '/pages/mine/mobile/mobile',
          })
        }, "2000");
      } else {
        setTimeout(function () {
          wx.navigateTo({
            url: '/pages/mine/bindcar/bindcar',
          })
        }, "2000");
      }

    } else {
      var that = this;
      var images = that.data.files;
      var timestamp = Date.parse(new Date()) / 1000;
      var random = Math.floor(Math.random() * 100);
      var sign = 'GHRDY65' + String(random) + String(timestamp);
      sign = md5.hexMD5(sign);
      wx.chooseImage({
        count: 9 - images.length,
        sizeType: ['original', 'compressed'],
        sourceType: ['album', 'camera'],
        success: function (res) {
          that.setData({
            chooseimg: true
          })
          // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
          for (var i = 0; i < res.tempFilePaths.length; i++) {
            wx.uploadFile({
              url: app.static.url + 'api/common/uploadFile',
              filePath: res.tempFilePaths[i],
              name: 'file',
              formData: {
                timestamp: timestamp,
                random: random,
                sign: sign
              },
              success: function (ret) {
                console.log(ret)
                var data = ret.data;
                data = JSON.parse(data);
                if (data.code == 1) {
                  that.setData({
                    files: that.data.files.concat(data.data.url),
                    
                  });
                }
              }
            })
          }
        }
      })
    }
  },
  // 预览图片
  previewImage: function (e) {
    wx.previewImage({
      current: e.currentTarget.id,
      urls: this.data.files
    })
  },
  //长按图片删除
  deleteImage: function (e) {
    var that = this;
    var images = that.data.files;
    var index = e.currentTarget.dataset.index; //获取当前长按图片下标
    wx.showModal({
      title: '提醒',
      content: '确定要删除此图片吗？',
      success: function (res) {
        if (res.confirm) {
          images.splice(index, 1);
        } else if (res.cancel) {
          return false;
        }
        that.setData({
          files: images
        });
      }
    })
  },
  // 图片删除效果
  closeClick: function (e) {
    var that = this;
    var images = that.data.files;
    var index = e.currentTarget.dataset.index; //获取当前按图片下标
    wx.showModal({
      title: '系统提醒',
      content: '确定要删除此图片吗？',
      success: function (res) {
        if (res.confirm) {
          images.splice(index, 1);
        } else if (res.cancel) {
          return false;
        }
        that.setData({
          files: images
        });
      }
    })

  },
  // 预览图片
  preview: function (e) {
    var index = e.currentTarget.dataset.index;
    wx.previewImage({
      current: this.data.files[index], // 当前显示图片的http链接  
      urls: this.data.files // 需要预览的图片http链接列表  
    })
  },
  // 预览音频
  previewVideo(e){
    var videofrom = e.currentTarget.dataset.videofrom;
    this.setData({
      previewvideo:true,
      videofrom:videofrom
    })
  },
  /**********用户地址选择**********/ 
  dingwei:function(){
    if (this.data.chexing.length == 0) {
      wx.showToast({
        title: '请先绑定爱车',
        icon: 'none',
        duration: 3000,
      })
      var status = wx.getStorageSync('status');
      console.log(status);
      if (status == 0) {
        setTimeout(function () {
          wx.navigateTo({
            url: '/pages/mine/mobile/mobile',
          })
        }, "2000");
      } else {
        setTimeout(function () {
          wx.navigateTo({
            url: '/pages/mine/bindcar/bindcar',
          })
        }, "2000");
      }

    } else {
      this.getUserLocation();
    }
  },
  
  /********用户授权********/
  getUserLocation: function () {
    let that = this
    wx.getSetting({
      success: (res) => {
        // res.authSetting['scope.userLocation'] == undefined    表示 初始化进入该页面
        // res.authSetting['scope.userLocation'] == false    表示 非初始化进入该页面,且未授权
        // res.authSetting['scope.userLocation'] == true    表示 地理位置授权
        // 拒绝授权后再次进入重新授权
        if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) {
          // console.log('authSetting:status:拒绝授权后再次进入重新授权', res.authSetting['scope.userLocation'])
          wx.showModal({
            title: '',
            content: '我们需要获取你的地理位置，请确认授权',
            success: function (res) {
              if (res.cancel) {
                wx.showToast({
                  title: '拒绝授权',
                  icon: 'none'
                })
              } else if (res.confirm) {
                wx.openSetting({
                  success: function (dataAu) {
                    // console.log('dataAu:success', dataAu)
                    if (dataAu.authSetting["scope.userLocation"] == true) {
                      //再次授权，调用wx.getLocation的API
                      that.getLocation(dataAu)
                    } else {
                      wx.showToast({
                        title: '授权失败',
                        icon: 'none'
                      })
                    }
                  }
                })
              }
            }
          })
        }
        // 初始化进入，未授权
        else if (res.authSetting['scope.userLocation'] == undefined) {
          // console.log('authSetting:status:初始化进入，未授权', res.authSetting['scope.userLocation'])
          //调用wx.getLocation的API
          that.getLocation(res)
        }
        // 已授权
        else if (res.authSetting['scope.userLocation']) {
          // console.log('authSetting:status:已授权', res.authSetting['scope.userLocation'])
          //调用wx.getLocation的API
          that.getLocation(res)
        }
      }
    })
  },
  // 微信获得经纬度
  getLocation: function (userLocation) {
    let that = this
    wx.getLocation({
      type: "wgs84",
      success: function (res) {
        // console.log('getLocation:success', res)
        var latitude = res.latitude
        var longitude = res.longitude
        that.moveToLocation();
      },
      fail: function (res) {
        // console.log('getLocation:fail', res)
        if (res.errMsg === 'getLocation:fail:auth denied') {
          wx.showToast({
            title: '拒绝授权',
            icon: 'none'
          })
          setTimeout(() => {
            wx.navigateBack()
          }, 1500)
          return
        }
        if (!userLocation || !userLocation.authSetting['scope.userLocation']) {
          that.getUserLocation()
        } else if (userLocation.authSetting['scope.userLocation']) {
          wx.showModal({
            title: '',
            content: '请在系统设置中打开定位服务',
            showCancel: false,
            success: result => {
              if (result.confirm) {
                wx.navigateBack()
              }
            }
          })
        } else {
          wx.showToast({
            title: '授权失败',
            icon: 'none'
          })
        }
      }
    })
  },
// 
  //移动选点
  moveToLocation: function () {
    var that = this;
    wx.chooseLocation({
      success: function (res) {
        if(res.name != ''){
          that.setData({
            address: res.name,
            latitude: res.latitude,
            longitude: res.longitude,
          })
        }
       
      },
      fail: function (err) {
        console.log(err);
      }
    });
  },

  // 需求提交
  xqtijiao:function(e){
    var param = {};
    var userid = wx.getStorageSync('userId');
    var description = e.detail.value.xiuchetext;
    var address = this.data.address;
    var longitude = this.data.longitude;
    var latitude = this.data.latitude;
    var index = this.data.index;
    var imgs = this.data.files;
    var video = this.data.videosrc;
    var voice = this.data.yuyinsrc;
    var voice_len = this.data.luyintime;
    console.log(index)

    if(address == ''){
        wx.showToast({
          title: '地址没有选择',
          icon:'none',
          duration:2000
        })
        return false;
    }
    if(index ==='' && this.data.oneche == false){
      wx.showToast({
        title: '请选择爱车',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    if(voice.length == 0 && imgs.length == 0 && video.length == 0 && description == ''){
      wx.showToast({
        title: '请填写至少一种描述',
        icon: 'none',
        duration: 2000
      })
      return false;
    }else{
      if(index == ''){
        var car_id = this.data.chexing[0].id;
      }else{
        var car_id = this.data.chexing[index].id;
      }
      
      var userid = wx.getStorageSync('userId');
      var imgs = imgs.join(",");
      var video = video.join(",");
      var voice = voice.join(",");
      var voice_len = voice_len.join(",");
      var param={
          user_id:userid,
          address:address,
          car_id:car_id,
          longitude:longitude,
          latitude:latitude,
          description:description,
          imgs:imgs,
          voice_len:voice_len,
          video:video,
          voice:voice,
      };
      // wx.showLoading({
      //   title: '提交中，请耐心等待。。。'
      // })
      if (wx.showLoading) {
        wx.showToast({
          icon: 'loading',
          title: '提交中，请耐心等待。。。',
          mask: true
        })
      };
      
          var that = this;
          var xuqiu={
            url: "api/index/uploadDemand",
            data:param,
            success:function(e){
                console.log(e);
                that.setData({
                  tixingshow: true
                })
                wx.hideLoading()
                myaudio.stop();
                if(e.data.code == 1){
                  that.setData({
                    on_demand:1,
                  })
                  that.geton(userid);
                  // that.getcarlist(userid); 
                  that.setData({
                    index:'',
                    files:[],
                    videosrc:[],
                    yuyinsrc:[],
                  })
                }
            }
          }
          app.post(xuqiu);

    }
    

  },

  // 补充需求
  buchongxq:function(){
    var demand_id = this.data.demanddetail.demand_id;
    wx.navigateTo({
      url: '/pages/index/buchong/buchong?id='+demand_id,
    })
  },
  // 取消需求
  quxiaoxq:function () {
    this.setData({
      dialogShow: true
    })
  },


  // 弹框
  tapDialogButton(e) {
    this.setData({
      dialogShow: false,
      showOneButtonDialog: false
    })
    if (e.detail.index == 0) {
      return false;
    }
    if (e.detail.index == 1) {
      var demand_id = this.data.demanddetail.demand_id;
      var that = this;
      var param = {
        url:"api/index/cancelDemand",
        data:{
          demand_id: demand_id
        },
        success:function(e){
            wx.showToast({
              title: e.data.msg,
              icon:'none',
              duration:2000
            })
          if (e.data.code == 1) {
              that.setData({
                on_demand:0,
                bjshangjia:[]
              })
              //this.onLoad();
              clearInterval(that.data.setinter);
          }
        }
      };
      app.post(param);
    }
  },

  // saoma
  saoma:function(){
    var _this = this;
    // 允许从相机和相册扫码
    wx.scanCode({
      success: (res) => {
        console.log(res)
        var path = res.path;
        wx.redirectTo({
          url:'/'+path,
        })
      }
    })
  },

  taptixing:function(e){
      console.log(e);
      this.setData({
        tixingshow:false
      })
  },
  // 视频预览关闭
  hideVideo:function(){
    this.setData({
      previewvideo:false
    })
  },


  // 完成服务
  finishit:function(){
    this.setData({
      finishShow:true
    })
  },
  tapFinishButton:function(e){
    this.setData({
      finishShow: false
    })
    if (e.detail.index == 0) {
      return false;
    }
    if (e.detail.index == 1) {
      var demand_id = this.data.demanddetail.demand_id;
      var that = this;
      var param = {
        url: "api/index/confirmEnd",
        data: {
          demand_id: demand_id
        },
        success: function (e) {
          wx.showToast({
            title: e.data.msg,
            icon: 'none',
            duration: 2000
          })
          if (e.data.code == 1) {
            that.setData({
              pingjiaShow:true
            })
            //this.onLoad();
            clearInterval(that.data.setinter);
          }
        }
      };
      app.post(param);
    }
  },
  // 评价
  tappingjiaButton:function(){
    this.setData({
      finishShow: false
    })
    if (e.detail.index == 0) {
      this.onLoad();
    }
    if (e.detail.index == 1) {
      wx.navigateTo({
        url: '/pages/mine/pingjia/pingjia?shopid=' + this.data.shop_id + '&demand_id=' + this.data.demanddetail.demand_id,
      })
    }
  },

  onHide:function(){
    app.globalData.detail = '';
  },
  onUnload:function(){
    app.globalData.detail = ''
  }
})
